﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace zykControl
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
{

    /// <summary>
    /// 专门用于和UIProxy配合的Windoes组件
    /// </summary>
    [ProvideProperty("DataField", typeof(Control))]
    [Designer(typeof(UIBindingDesigner), typeof(IDesigner))]
    public class UIBinding : Component, IExtenderProvider
    {
        private Dictionary<Control, DataFieldItem[]> mFields = [];
        // Dim mSources As New Dictionary(Of Control, String)

        public UIBinding()
        {
        }

        public bool CanExtend(object extendee)
        {
            if (extendee is Form)
            {
                return false;
            }
            else if (extendee is Control)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        [Description("指示此控件绑定到模型对象的成员名称")]
        [Editor(typeof(UIBindingEditer), typeof(UITypeEditor))]
        public DataFieldItem[]? GetDataField(Control theControl)
        {
            if (theControl is null)  return null;
            if (mFields is not null && mFields.TryGetValue(theControl, out DataFieldItem[]? value))
            {
                return value;
            }
            else
            {
                return [];
            }
        }

        public void SetDataField(Control theControl, DataFieldItem[] theValue)
        {
            if (theControl is null || theValue is null) return;
            mFields ??= [];
            if (!mFields.TryAdd(theControl, theValue))
            {
                mFields[theControl] = theValue;
            }
        }

        [Description("指示此组件绑定的模型对象的类名")]
        public string ObjectName { get; set; } = "";

        public IEnumerable<Control> GetControls()
        {
            return mFields.Keys;
        }
    }

    [Serializable]
    [DefaultProperty("PropertyName")]
    [DesignTimeVisible(false)]
    [ToolboxItem(false)]
    public class DataFieldItem
    {
        public string PropertyName { get; set; } = "";
        public string DataField { get; set; } = "";
        public DataSourceUpdateMode UpdateMode { get; set; } = DataSourceUpdateMode.OnValidation;
        public string FormatString { get; set; } = "";
        public string NullText { get; set; } = "";

        public override string ToString()
        {
            string dText = PropertyName;
            switch (UpdateMode)
            {
                case DataSourceUpdateMode.OnPropertyChanged:
                    {
                        dText += " == ";
                        break;
                    }

                case DataSourceUpdateMode.Never:
                    {
                        dText += " < ";
                        break;
                    }

                default:
                    {
                        dText += " = ";
                        break;
                    }
            }

            dText += DataField;
            if (!string.IsNullOrEmpty(FormatString))
            {
                dText += " (" + FormatString + ")";
            }

            if (!string.IsNullOrEmpty(NullText))
            {
                dText += " [" + NullText + "]";
            }

            return dText;
        }
    }

    public class UIBindingEditer : UITypeEditor
    {
        public override object? EditValue(ITypeDescriptorContext? context, IServiceProvider provider, object? value)
        {
            if (context == null) return value;
            var dValue = FrmUIBindingEditer.ShowA(context, provider, value);
            if (dValue is null) return value;
            return dValue;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }

    public class UIBindingDesigner : ComponentDesigner
    {
        public UIBindingDesigner()
        {
            var dVerb = new DesignerVerb("数据绑定项总览", (sender, e) => ViewTotal());
            Verbs.Add(dVerb);
        }

        private void ViewTotal()
        {
            UIBinding dComponent = (UIBinding)Component;
            FrmUIBindingTotal.ShowA(dComponent);
        }
    }
}